package com.zzb.text.controller;

import com.zzb.text.entity.Menu;
import com.zzb.text.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by ZZB on 2017/6/16.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @RequestMapping("/index")
    public String list(Model model){
        List<Menu> list = menuService.list();
        model.addAttribute("list",list);
        return "/menu/index";
    }

    @RequestMapping("/form")
    public String form(Menu menu, Model model){
        String id = menu.getId();
        if(id!=null){
            menu = menuService.get(id);
        }
        model.addAttribute("menu",menu);
        return "/menu/form";
    }

    @RequestMapping("/save")
    public String save(Menu menu){
        menuService.save(menu);
        return "redirect:/menu/index";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        menuService.delete(id);
        return "redirect:/menu/index";
    }

}
